package com.wenwei.sharebox.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.bean.GoodsInfoBean;
import com.wenwei.sharebox.bean.OrderGoodsBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.view.PayPopupWindow;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/12.
 */
public class ShareConfirmActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.iv_select_zu)
    ImageView ivSelectZu;
    @Bind(R.id.ll_select_zu)
    LinearLayout llSelectZu;
    @Bind(R.id.iv_select_buy)
    ImageView ivSelectBuy;
    @Bind(R.id.ll_select_buy)
    LinearLayout llSelectBuy;
    @Bind(R.id.content_p)
    RelativeLayout contentPll;
    @Bind(R.id.goods_name)
    TextView goodsName;
    @Bind(R.id.goods_img)
    ImageView goodsImg;
    @Bind(R.id.jie_suan_ok)
    TextView jieSuanTv;

    GoodsInfoBean infoBean;
    @Bind(R.id.zu_price)
    TextView zuPrice;
    @Bind(R.id.zu_yu_price)
    TextView zuYuPrice;
    @Bind(R.id.buy_price)
    TextView buyPrice;
    @Bind(R.id.total_price)
    TextView totalPrice;
    @Bind(R.id.share_confirm_bottom)
    RelativeLayout shareConfirmBottom;

    private int orderType = -1;//生成订单类型

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_share_confirm;
    }

    private void setInfo() {
        Glide.with(mContext).load(infoBean.getImg()).into(goodsImg);
        goodsName.setText(infoBean.getShopName());
        if (infoBean.getIsBuy() == 0) {
            llSelectBuy.setVisibility(View.GONE);
        }
        if (infoBean.getIsRent() == 0) {
            llSelectZu.setVisibility(View.GONE);
        }
        zuPrice.setText(infoBean.getIntegralNum()+"积分/天");
        zuYuPrice.setText("押金：￥"+infoBean.getDeposit());
        buyPrice.setText("售价：￥"+infoBean.getMoney());
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("共享确认");
        setSelect(-1);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.base_back, R.id.jie_suan_ok, R.id.ll_select_buy, R.id.ll_select_zu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.jie_suan_ok:
                if(infoBean!=null){
                    createOrder();
                }
//                new AuthenticationPopup(ShareConfirmActivity.this,contentPll, new AuthenticationPopup.CallBack() {
//                    @Override
//                    public void onRightClick() {
//
//                    }
//
//                    @Override
//                    public void onLeftClick() {
//
//                    }
//                });
                break;
            case R.id.ll_select_buy:
                if (infoBean.getIsRent() != 0) {
                    setSelect(2);
                } else {
                    ivSelectZu.setVisibility(View.GONE);
                    ivSelectBuy.setVisibility(View.VISIBLE);
                    llSelectZu.setBackgroundResource(R.drawable.bc_input_msg);
                    llSelectBuy.setBackgroundResource(R.drawable.bc_confirm_select);
                }
                orderType = 1;
                jieSuanTv.setBackgroundColor(Color.parseColor("#EE2F36"));
                jieSuanTv.setEnabled(true);
                totalPrice.setText("总价：￥"+infoBean.getMoney());
                break;
            case R.id.ll_select_zu:
                orderType = 0;
                if (infoBean.getIsBuy() != 0) {
                    setSelect(1);
                } else {
                    ivSelectZu.setVisibility(View.VISIBLE);
                    ivSelectBuy.setVisibility(View.GONE);
                    llSelectZu.setBackgroundResource(R.drawable.bc_confirm_select);
                    llSelectBuy.setBackgroundResource(R.drawable.bc_input_msg);
                }
                jieSuanTv.setBackgroundColor(Color.parseColor("#EE2F36"));
                jieSuanTv.setEnabled(true);
                totalPrice.setText("总价："+infoBean.getIntegralNum()+"积分，押金：￥"+infoBean.getDeposit());
                break;
        }
    }


    private void setSelect(int position) {

        if (position == 1) {
            ivSelectZu.setVisibility(View.VISIBLE);
            ivSelectBuy.setVisibility(View.GONE);
            llSelectZu.setBackgroundResource(R.drawable.bc_confirm_select);
            llSelectBuy.setBackgroundResource(R.drawable.bc_input_msg);
        } else if (position == 2) {
            ivSelectZu.setVisibility(View.GONE);
            ivSelectBuy.setVisibility(View.VISIBLE);
            llSelectZu.setBackgroundResource(R.drawable.bc_input_msg);
            llSelectBuy.setBackgroundResource(R.drawable.bc_confirm_select);
        } else {
            ivSelectZu.setVisibility(View.GONE);
            ivSelectBuy.setVisibility(View.GONE);
            llSelectZu.setBackgroundResource(R.drawable.bc_input_msg);
            llSelectBuy.setBackgroundResource(R.drawable.bc_input_msg);

        }
    }

    @Override
    protected void initData() {
        super.initData();
        getGoodsInfo();
    }

    //获取商品详情
    private BaseSubscriber<GoodsInfoBean> goodsInfoSubscriber;

    private void getGoodsInfo() {
        goodsInfoSubscriber = new BaseSubscriber<GoodsInfoBean>(mContext) {
            @Override
            public void onSuccess(GoodsInfoBean goodsInfoBean) {
                infoBean = goodsInfoBean;
                setInfo();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                llSelectZu.setVisibility(View.GONE);
                llSelectBuy.setVisibility(View.GONE);
            }
        };
        try {
            JSONObject jb = new JSONObject();
            jb.put("device_unique_num", getIntent().getStringExtra("deviceCode"));
            ApiManager.getInstance().deviceUniqueNum(goodsInfoSubscriber, getIntent().getStringExtra("deviceCode"));
        } catch (Exception e) {
        }
    }


    //生成订单
    private BaseSubscriber<OrderGoodsBean> orderSubscriber;

    private void createOrder() {
        orderSubscriber = new BaseSubscriber<OrderGoodsBean>(mContext, true, "请稍后...") {
            @Override
            public void onSuccess(OrderGoodsBean orderGoodsBean) {
                 new PayPopupWindow(ShareConfirmActivity.this, contentPll, new PayPopupWindow.CallBack() {
                    @Override
                    public void onPayClick() {
                        goActivity(mContext, PaySuccessActivity.class);   //
                    }
                },orderGoodsBean.getOrderNumber());

            }
        };
        ApiManager.getInstance().createOrder(orderSubscriber, infoBean.getId(), orderType);
    }


}
